package com.frxuexi.shop.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.base.db.ShopLoveEntity
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.UiState
import com.frxuexi.shop.R
import com.frxuexi.shop.adapter.ShopLoveAdapter
import com.frxuexi.shop.databinding.ActivityShopLoveBinding
import com.frxuexi.shop.intent.ShopIntent
import com.frxuexi.shop.viewmodel.ShopViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/shop/ShopLoveActivity")
@AndroidEntryPoint
class ShopLoveActivity : BaseActivity<ActivityShopLoveBinding,ShopIntent,ShopViewModel>() {
    val shopLoveAdapter by lazy { ShopLoveAdapter() }
    override fun getVM(): ShopViewModel {
        return viewModels<ShopViewModel>().value
    }

    override fun getBind(): ActivityShopLoveBinding {
        return ActivityShopLoveBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val entities = it.data as Collection<ShopLoveEntity>
                        shopLoveAdapter.addData(entities)
                        shopLoveAdapter.notifyDataSetChanged()
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.shopLove.apply {
            adapter=shopLoveAdapter
            layoutManager=StaggeredGridLayoutManager(2,RecyclerView.VERTICAL)
        }

        vm.sendIntent(ShopIntent.selectShops)
    }

}